<?php
namespace App\Services\Fund\FundOptionLog;
use App\Services\Fund\ValidateFundOptionLog;
use App\Models\Fund\FundOptionLog;
use App\Models\Fund\FundFreezeLog;
use App\Models\Fund\CurrencyFreezeLog;
use App\Models\Fund\IntegralFreezeLog;
use App\Models\Fund\FundUnfreezeLog;
use App\Models\Fund\CurrencyUnfreezeLog;
use App\Models\Fund\IntegralUnfreezeLog;
use App\Models\Fund\OperateReason;
use Illuminate\Support\Facades\DB;
class FundOptionLogLogic
{
    // 所有用户资金操作日志分页数据
    public function list(array $data): array
    {
        $keywords = $data['keywords'];
        $list = FundOptionLog::where('status','=',1);
        !empty($data['type']) && $list->where('type','=',$data['type']);
         if(!empty($data['keywords'])){
            $list->where(function ($query) use ($keywords) {
                $query->orWhere('remark', 'like',"%{$keywords}%")
                      ->orWhere('user_id','like',"%{$keywords}%");
               });
        }
         if(!empty($data['startTime'])){
            if(!empty($data['endTime'])){
                  $list->whereBetween('create_time', [$data['startTime'], $data['endTime']]);
            }else{
                  $list->where('create_time','>=',$data['startTime']);
            }
        }else{
            if(!empty($data['endTime'])){
                $list->where('create_time','<=',$data['endTime']);
            }
        }
        $res = $list->orderBy('id', 'desc')->paginate($data['list_rows'], null, null, $data['page'])->toArray();
        return auto_return_page($res, '已找到用户资金操作日志', '没有用户资金操作日志');
    }
    // 获取某些user_ids的的资金操作日志(分页)
    public function listByUserIds(array $data): array
    {
        $_where['status'] = 1;
        if ($data['type']) {
            $_where['type'] = $data['type'];
        }
        $res = FundOptionLog::with('waybill')
            ->where($_where)
            ->whereIn('user_id',[$data['user_ids']])
            ->orderBy('id', 'desc')
            ->paginate($data['list_rows'], null, null, $data['page'])
            ->toArray();
        return auto_return_page($res, '已找到用户资金操作日志', '没有用户资金操作日志');
    }
    // 获取某个用户某个状态的资金操作日志分页数据
    public function listByUserType(array $data): array
    {
        $query = FundOptionLog::where([['status','=',1],['user_id','=',$data['user_id']]]);
        if(isset($data['type']) && !empty($data['type'])){ $query->where('type','=',$data['type']);}
        if(isset($data['money'])){
            if($data['money'] == 'income'){
                 $query->where('money','>',0);
            }elseif($data['money'] == 'expend'){
                 $query->where('money','<',0);
            }
        }
        if(isset($data['time'])){
            $threeMonthTime = mktime(0, 0, 0, date("m")-3, date("d"), date("Y"));
            if($data['time'] == 'threeMonthAgo'){
                $query->where('create_time','<',$threeMonthTime);  //3个月以前
            }elseif($data['time'] == 'threeMonth'){
                $query->where('create_time','>',$threeMonthTime);    //3个月之内
            }
        }
        $res =  $query->orderBy('id', 'desc')->paginate($data['list_rows'], null, null, $data['page'])->toArray();
        return auto_return_page($res, '已找到用户资金操作日志', '没有用户资金操作日志');
    }

    /*获取管理员冻结资金、积分、亚数币的原因记录*/
    public function freezeOperateReason($data): array
    {
        $keywords = $data['keywords'];
        $res =OperateReason::where('status','=',1);
        !empty($data['type']) && $res->where('type','=',$data['type']);
        if(!empty($data['keywords'])){
           $res->where(function ($query) use ($keywords) {
                $query->orWhere('reason', 'like',"%{$keywords}%")
                      ->orWhere('user_info->name','like',"%{$keywords}%")
                      ->orWhere ('opposite_info->name','like',"%{$keywords}%");
               });
        }
        $list = $res->orderBy('id', 'desc')->paginate($data['list_rows'], null, null, $data['page'])->toArray();
        if($list['total'] == 0){
            return verify_err(null, '没有符合条件的记录');
        }
        foreach ($list['data'] as $k => &$v) {
            $_whereLog['type'] = 'system';
            $_whereLog['app_id'] = $v['app_id'];
            $_whereLog['user_id'] = $v['user_id'];
            $_whereLog['obj_id'] = $v['id'];
            $_whereLog['status'] = 1;
            $_whereLog['freeze_counts'] = 1;  //一条原因只冻结一次
            if($v['type'] == 'freeze_fund'){
                $v['freeze_status'] = FundFreezeLog::where($_whereLog)->value('freeze_status');
            }elseif($v['type'] == 'freeze_currency'){
                $v['freeze_status'] = CurrencyFreezeLog::where($_whereLog)->value('freeze_status');
            }elseif($v['type'] == 'freeze_integral'){
                $v['freeze_status'] = IntegralFreezeLog::where($_whereLog)->value('freeze_status');
            }else{
                $v['freeze_status'] = -1;
            }
        }
        return auto_return_page($list, '成功获取管理员冻结原因记录表');
    }
    /*获取某条原因记录相关的解冻详情*/
    public function reasonUnfreezeInfo($data): array
    {
        $_where['type'] = 'system';
        $_where['app_id'] = $data['app_id'];
        $_where['user_id'] = $data['user_id'];
        $_where['obj_id'] = $data['obj_id'];
        $_where['status'] = 1;
        $_where['freeze_counts'] = 1;
        if($data['action'] == 'currency'){
            $info = CurrencyFreezeLog::where($_where)->first();
            if($info){
                $info['unfreeze_reason'] = CurrencyUnfreezeLog::where('freeze_log_id','=',$info['id'])->value('remark');
            }
        }elseif($data['action'] == 'fund'){
             $info = FundFreezeLog::where($_where)->first();
            if($info){
                $info['unfreeze_reason'] = FundUnfreezeLog::where('freeze_log_id','=',$info['id'])->value('remark');
            }
        }elseif($data['action'] == 'integral'){
            $info = IntegralFreezeLog::where($_where)->first();
            if($info){
                $info['unfreeze_reason'] = IntegralUnfreezeLog::where('freeze_log_id','=',$info['id'])->value('remark');
            }
        }
        return auto_return($info);

    }
}
